//
//  WJEssAndNewPLVC.m
//  WJBSApp
//
//  Created by dllo on 16/7/22.
//  Copyright © 2016年 王景伟. All rights reserved.
//
// 上拉加载和下拉刷新
// 抓个评论的接口-->id是主接口里面的id
// http://api.budejie.com/api/api_open.php?&a=dataList&c=comment&page=1&data_id=19446453

#import "WJEssAndNewPLVC.h"
#import "WJFowardVC.h"
#import "WJEssAndNewBaseCell.h"
#import "WJPLCell.h"
#import "WJPLmodel.h"
#import "WJLastViewController.h" // 关注界面


@interface WJEssAndNewPLVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) WJBaseTableView *tableView;

@property (nonatomic,strong) NSMutableArray *array;

@property (nonatomic, assign) NSInteger pageId;

@property (nonatomic, assign) BOOL isDown;

@end

@implementation WJEssAndNewPLVC

static NSString *const PLEssCell = @"PLEssCell";

static NSString *const PLCell = @"PLCell";

-(void)dealloc
{
    [_model release];
    [_tableView release];
    [_array release];
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    [super dealloc];
}

//每次进来都要执行
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
    
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.array = [NSMutableArray array];

    [self config];

    [self createView];
    
    [self createData];
}

- (void)config
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"评论";
    // 右面的分享按钮
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"comment_nav_item_share_icon@3x.png" hightImage:@"comment_nav_item_share_icon_click@3x.png" target:self action:@selector(rightAction)];

}

#pragma mark - 点击右面按钮进入分享界面
- (void)rightAction
{
    WJFowardVC *vc = [[WJFowardVC alloc]init];
    
    vc.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.6];
    
    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)createView
{
    
    self.tableView = [[WJBaseTableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[WJEssAndNewBaseCell class] forCellReuseIdentifier:PLEssCell];
    [self.tableView registerClass:[WJPLCell class] forCellReuseIdentifier:PLCell];
    [_tableView release];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isDown = YES;
        [self createData];
        [self.tableView.mj_header endRefreshing];
        
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.isDown = NO;
        [self createData];
        [self.tableView.mj_footer endRefreshing];
        
    }];


}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 ) {
        
        WJEssAndNewBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:PLEssCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.model;
        cell.tagsVC.tagBlock = ^(NSInteger ID,NSString *name){
            
        };
        return cell;
        
    }else{
        
#pragma mark - 点击人头像 进入关注界面
        
        WJPLCell *cell = [tableView dequeueReusableCellWithIdentifier:PLCell];
        
        WJPLmodel *model = self.array[indexPath.row];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        __block typeof(self) weakSelf = self;
        cell.followIDBlock = ^(NSString *userID,NSString *userName){
            
            WJLastViewController *vc = [[WJLastViewController alloc]init];
            
            vc.userID = userID;
            
            vc.userName = userName;
            
            [weakSelf.navigationController pushViewController:vc animated:YES];
            
            [vc release];
            
        };
        
        cell.model = model;
        
        return cell;
        
    }
}



- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return nil;
    }
        return @"最新评论";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return self.model.cellHeight;
    }
     WJPLmodel *model = self.array[indexPath.row];
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:16]};
    CGRect rect = [model.content boundingRectWithSize:CGSizeMake( WIDTH - 20, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return (HEIGHT/9 - 20)/2 + 10 +  rect.size.height;
}







- (void)createData
{
    if (self.isDown) {
        self.pageId = 1;
    } else {
        self.pageId++;
    }

    NSString *url = [NSString stringWithFormat:@"http://api.budejie.com/api/api_open.php?&a=dataList&c=comment&page=%ld&data_id=%@",(long)self.pageId,self.model.id];
    [WJNetTool GET:url body:nil header:nil response:WJJSON success:^(id resuposeObject) {

#pragma mark - 解决BUG 厉害?????????????????????????????????????
        
        if(![resuposeObject isKindOfClass:[NSDictionary class]])
        {
            self.tableView.mj_header.hidden = YES;
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        
        NSDictionary *dic = resuposeObject;
        
        NSArray *array = dic[@"data"];
        
        if (self.isDown) {
            
            [self.array removeAllObjects];
            
        }
        
        for (NSDictionary *dic in array) {
            
            WJPLmodel *model = [[WJPLmodel alloc]initWithDic:dic];
            
            model.userModel = [[WJPLUserModel alloc]initWithDic:model.user];
            
            [self.array addObject:model];
            
            [model release];
            
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {}];
    
    
    
}

































- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
